package org.jeecg.modules.wlhy.hycarrier.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.wlhy.hycarrier.entity.HyCarrierDriver;
import org.jeecg.modules.wlhy.hycarrier.service.IHyCarrierDriverService;
import org.jeecg.modules.wlhy.hycarrier.service.IHyCarrierService;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverService;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicle;
import org.jeecg.modules.wlhy.hyvehicle.service.IHyVehicleService;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: hy_carrier_driver
 * @Author: jeecg-boot
 * @Date:   2024-11-01
 * @Version: V1.0
 */
@Api(tags="hy_carrier_driver")
@RestController
@RequestMapping("/carrier/hyCarrierDriver")
@Slf4j
public class HyCarrierDriverController extends JeecgController<HyCarrierDriver, IHyCarrierDriverService> {
	@Autowired
	private IHyCarrierDriverService hyCarrierDriverService;

	@Autowired
	private UserUtil userUtil;

	@Autowired
	private ISysUserService userService;

	@Autowired
	private IHyCarrierService carrierService;

	@Autowired
	private IHyDriverService driverService;

	@Autowired
	private IHyVehicleService vehicleService;

	/**
	 * 分页列表查询
	 *
	 * @param hyCarrierDriver
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="hy_carrier_driver-分页列表查询", notes="hy_carrier_driver-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(HyCarrierDriver hyCarrierDriver,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
		QueryWrapper<HyCarrierDriver> queryWrapper = QueryGenerator.initQueryWrapper(hyCarrierDriver, req.getParameterMap());
		Page<HyCarrierDriver> page = new Page<HyCarrierDriver>(pageNo, pageSize);
		IPage<HyCarrierDriver> pageList = hyCarrierDriverService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param hyCarrierDriver
	 * @return
	 */
	@AutoLog(value = "hy_carrier_driver-添加")
	@ApiOperation(value="hy_carrier_driver-添加", notes="hy_carrier_driver-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody HyCarrierDriver hyCarrierDriver) {
		String tel = carrierService.getById(hyCarrierDriver.getCarrierId()).getTel();
		if (StringUtils.isEmpty(tel)){
			return Result.error("承运商手机号不能为空");
		}
		hyCarrierDriver.setUserId(userService.getUserByPhone(tel).getId());
		hyCarrierDriver.setCreateBy(userUtil.getLoginUser().getUsername());
		hyCarrierDriver.setCreateTime(new Date());
		hyCarrierDriverService.save(hyCarrierDriver);
		HyDriver driver = new HyDriver();
		driver.setId(hyCarrierDriver.getDriverId());
		driver.setCarrierId(hyCarrierDriver.getCarrierId());
		driverService.updateById(driver);
		List<HyVehicle> hyVehicles = vehicleService.getVehicleByDriverId(hyCarrierDriver.getDriverId());
		for (HyVehicle hyVehicle : hyVehicles) {
			hyVehicle.setCarrierId(hyCarrierDriver.getCarrierId());
		}
		vehicleService.updateBatchById(hyVehicles);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param hyCarrierDriver
	 * @return
	 */
	@AutoLog(value = "hy_carrier_driver-编辑")
	@ApiOperation(value="hy_carrier_driver-编辑", notes="hy_carrier_driver-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody HyCarrierDriver hyCarrierDriver) {
		hyCarrierDriver.setUserId(userUtil.getLoginUser().getId());
		hyCarrierDriver.setUpdateBy(userUtil.getLoginUser().getUsername());
		hyCarrierDriver.setUpdateTime(new Date());
		hyCarrierDriverService.updateById(hyCarrierDriver);

		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "hy_carrier_driver-通过id删除")
	@ApiOperation(value="hy_carrier_driver-通过id删除", notes="hy_carrier_driver-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		hyCarrierDriverService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "hy_carrier_driver-批量删除")
	@ApiOperation(value="hy_carrier_driver-批量删除", notes="hy_carrier_driver-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.hyCarrierDriverService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="hy_carrier_driver-通过id查询", notes="hy_carrier_driver-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		HyCarrierDriver hyCarrierDriver = hyCarrierDriverService.getById(id);
		if(hyCarrierDriver==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(hyCarrierDriver);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param hyCarrierDriver
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HyCarrierDriver hyCarrierDriver) {
        return super.exportXls(request, hyCarrierDriver, HyCarrierDriver.class, "hy_carrier_driver");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HyCarrierDriver.class);
    }

}
